﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using FingerTip.Sms.Aliyun.Helper;
using FingerTip.Sms.Aliyun.Internal;
using FingerTip.Sms.Core.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace FingerTip.Sms.Aliyun
{
    public class AliyunSmsClient
    {
        public IAliyunSmsSetting AliyunSmsSetting { get; private set; }
        public IAcsClient AcsClient { get; private set; }

        public AliyunSmsClient()
        {
            var aliyunSmsSetting = AliyunSmsHelper.GetSettingsFunc();

            if (aliyunSmsSetting == null)
            {
                throw new SmsException("The Aliyun Sms Config is null!");
            }
            if (string.IsNullOrWhiteSpace(aliyunSmsSetting.SignName))
            {
                throw new SmsException("The SignName is null!");
            }
            try
            {
                AliyunSmsHelper.LoggerAction("Debug", "SmsSettings:" + JsonConvert.SerializeObject(aliyunSmsSetting));
                AliyunSmsSetting = aliyunSmsSetting;
                //var product = aliyunSmsSettting.Product;//短信API产品名称（短信产品名固定，无需修改）
                //var domain = aliyunSmsSettting.Domain;//短信API产品域名（接口地址固定，无需修改）
                //var accessKeyId = aliyunSmsSettting.AccessKeyId;//你的accessKeyId，参考本文档步骤2
                //var accessKeySecret = aliyunSmsSettting.AccessKeySecret;//你的accessKeySecret，参考本文档步骤2

                var profile = DefaultProfile.GetProfile(aliyunSmsSetting.RegionId, aliyunSmsSetting.AccessKeyId, aliyunSmsSetting.AccessKeySecret);
                DefaultProfile.AddEndpoint(aliyunSmsSetting.EndpointName, aliyunSmsSetting.RegionId, aliyunSmsSetting.Product, aliyunSmsSetting.Domain);
                AcsClient = new DefaultAcsClient(profile);
                AliyunSmsHelper.LoggerAction("Debug", "已创建AcsClient。");
            }
            catch (Exception ex)
            {
                AliyunSmsHelper.LoggerAction("Error", ex.ToString());
                throw new SmsException("配置出错,请检查配置!");
            }
        }
    }
}
